package edu.ynu.MyProject.controller;

import edu.ynu.MyProject.common.controller.LogicController;
import edu.ynu.MyProject.dao.IGreenHouseDAO;
import edu.ynu.MyProject.dto.GreenHouseDTO;
import edu.ynu.MyProject.entity.GreenHouseInfo;
import edu.ynu.MyProject.service.GreenHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "大棚实体")
@RestController
@RequestMapping("/greenhouse")
public class GreenHouseController extends LogicController<GreenHouseService, IGreenHouseDAO, GreenHouseInfo, Long> {

    public GreenHouseController(@Autowired GreenHouseService greenHouseService) {
        super(greenHouseService);
    }

    @ApiOperation("通过大棚ID获取大棚的详细信息")
    @GetMapping("/info/{greenhouseId}")
    public ResponseEntity<GreenHouseDTO> getGreenhouseInfo(@PathVariable Long greenhouseId) {
        try {
            // 获取信息
            GreenHouseDTO greenhouseDto = service.getInfo(greenhouseId);
            return ResponseEntity.ok(greenhouseDto);
        } catch (RuntimeException e) {
            // 找不到大棚，报错
            return ResponseEntity.status(HttpStatus.NOT_FOUND)
                    .body(null);
        }
    }

    @ApiOperation("通过大棚ID获取大棚的简要信息")
    @GetMapping("/summary/{greenhouseId}")
    public Map<String, Object> getSummaryInfo(@PathVariable Long greenhouseId) {
        GreenHouseDTO greenhouseDTO = service.getInfo(greenhouseId);
        Map<String, Object> result = new HashMap<>();
        result.put("name", greenhouseDTO.getName());
        result.put("deviceCount", greenhouseDTO.getDeviceCount());
        return result;
    }

    @ApiOperation("获取所有设备")
    @GetMapping("/devices/{greenhouseId}")
    public List<List<Object>> getAllDevices(@PathVariable Long greenhouseId) {
        return service.getAllDevices(greenhouseId);
    }
}